package com.gr.smartedubackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.smartedubackend.common.BaseResponse;
import com.gr.smartedubackend.common.DeleteRequest;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.model.dto.course.CourseAddRequest;
import com.gr.smartedubackend.model.dto.course.CourseQueryRequest;
import com.gr.smartedubackend.model.dto.course.CourseUpdateRequest;
import com.gr.smartedubackend.model.entity.Course;
import com.gr.smartedubackend.service.CourseService;
import com.gr.smartedubackend.utils.ResultUtils;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @PostMapping("/add")
    public BaseResponse<Long> addCourse(@RequestBody CourseAddRequest courseAddRequest) {
        ThrowUtils.throwIf(courseAddRequest == null, ErrorCode.PARAMS_ERROR);
        Course course = new Course();
        BeanUtils.copyProperties(courseAddRequest, course);
        boolean result = courseService.save(course);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(course.getId());
    }

    @GetMapping("/get")
    public BaseResponse<Course> getCourseById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Course course = courseService.getById(id);
        ThrowUtils.throwIf(course == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(course);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<Course>> listCourseByPage(@RequestBody CourseQueryRequest courseQueryRequest) {
        ThrowUtils.throwIf(courseQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = courseQueryRequest.getCurrent();
        int pageSize = courseQueryRequest.getPageSize();
        Page<Course> coursePage = courseService.page(new Page<>(current, pageSize),
                courseService.getQueryWrapper(courseQueryRequest));
        return ResultUtils.success(coursePage);
    }

    @PostMapping("/list")
    public BaseResponse<List<Course>> listCourse(@RequestBody CourseQueryRequest courseQueryRequest) {
        ThrowUtils.throwIf(courseQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(courseService.list(courseService.getQueryWrapper(courseQueryRequest)));
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateCourse(@RequestBody CourseUpdateRequest courseUpdateRequest) {
        ThrowUtils.throwIf(courseUpdateRequest == null || courseUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        Course course = new Course();
        BeanUtils.copyProperties(courseUpdateRequest, course);
        boolean result = courseService.updateById(course);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteCourse(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = courseService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }
}
